package priv.demos.demo1.anno;

import com.alibaba.fastjson.JSON;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import priv.demos.demo1.anno.annos.Clean;
import priv.demos.demo1.anno.annos.Param;
import priv.demos.demo1.anno.enums.FunctionEnum;
import priv.demos.demo1.anno.service.MockService;
import priv.demos.demo1.anno.service.ProductionService;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

/**
 * Created on 2017/3/1.
 */
public class Main {
    public static void main(String[] args) {

        String str = "按功能操作缓存::{\"args\":{\"productionCode\":\"KT10009\"},\"cacheOpType\":\"delete\",\"functionEnum\":\"PRODUCTION\",\"type\":\"CacheOpAsFunction\"}";
        System.out.println("str.length() = " + str.length());
    }

    void foo() {
        System.out.println("foo");
    }

    static void test3() {
        try {
            Method foo = Main.class.getDeclaredMethod("foo",null);
            foo.invoke(new Main(),null);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    static void test2() {

        Class<?> clazz = MockService.class;
        String basePackage = "priv.demos.demo1.anno";
        Package aPackage = Package.getPackage(basePackage);
        display(aPackage);

        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Clean.class);
        context.scan(basePackage);
    }

    static void test1() {

        Class<?> aClass = FunctionEnum.class;
        boolean instance = aClass.isInstance(Enum.class);
        Object[] enumConstants = aClass.getEnumConstants();
        System.out.println("enumConstants = " + JSON.toJSONString(enumConstants));
        System.out.println(enumConstants[0].getClass());


        ProductionService[] enumConstants1 = ProductionService.class.getEnumConstants();
        System.out.println("enumConstants = " + JSON.toJSONString(enumConstants1));


        System.out.println("instance = " + instance);

        try {
            Class<ProductionService> clazz = ProductionService.class;
            Method getProduction = clazz.getMethod("getProduction", new Class[]{String.class});

            Class<?>[] parameterTypes = getProduction.getParameterTypes();
            boolean annotationPresent = parameterTypes[0].isAnnotationPresent(Param.class);
            System.out.println("parameterTypes = " + parameterTypes[0]);
            System.out.println("annotationPresent = " + annotationPresent);
            Annotation[][] parameterAnnotations = getProduction.getParameterAnnotations();
            Annotation annotation = parameterAnnotations[0][0];
            Param param = (Param) annotation;
            String value = param.value();
            System.out.println(value);
            System.out.println(" = " + JSON.toJSONString(getProduction.getParameterAnnotations()));
            System.out.println(" = " + JSON.toJSONString(getProduction.getGenericParameterTypes()));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static void display(Object o) {
        System.out.println(JSON.toJSONString(o));
    }
}
